import { isAuth } from "@/helps/auth";
import React, { ReactElement } from "react";
import { Redirect, Route, RouteProps } from "react-router-dom";

interface Props extends RouteProps {
  component: React.ComponentType<any>;
}

export default function PrivateRoute({
  component: Component, // react的组件名必须大写
  ...rest
}: Props): ReactElement {
  return (
    <Route
      {...rest}
      render={(props) => {
        const auth = isAuth();
        console.log("props", props); //withRouter的props
        if (auth) {
          return <Component {...props} />;
        }
        return <Redirect to="/signin" />;
      }}
    />
  );
}
